#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include "Common.hpp"

const std::string Sep = "\r\n";
const std::string LineSep = " ";
const std::string BlankLine = Sep;

// B/S
class HttpRequest
{
public:
    HttpRequest() {}
    ~HttpRequest() {}

    // GET /favicon.ico HTTP/1.1\r\n
    // Host: 8.137.19.140:8080
    // Connection: keep-alive
    // User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
    // Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
    // Referer: http://8.137.19.140:8080/?msg=i_have_sent_a_message_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_he_
    // Accept-Encoding: gzip, deflate
    // Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
    // dnt: 1
    // sec-gpc: 1
    //
    void Deserialize(std::string &request_str)
    {
        if (ParseOneLine(request_str, &_req_line, Sep))
        {
            // 提取请求行中的详细字段
            ParseReqLine(_req_line, LineSep);
        }
    }
private:
    // GET /favicon.ico HTTP/1.1 -> GET    /favicon.ico      HTTP/1.1
    void ParseReqLine(std::string &_req_line, const std::string sep)
    {
        std::stringstream ss(_req_line);
        ss >> _method >> _uri >> _version;
    }
private:
    std::string _req_line;
    std::vector<std::string> _req_header;
    std::string _blank_line;
    std::string _body;

    // 在反序列化的过程中，细化我们解析出来的字段
    std::string _method;
    std::string _uri;
    std::string _version;
};

class HttpResponse
{
private:
    std::string _resp_line;
    std::vector<std::string> _resp_header;
    std::string _blank_line;
    std::string _body;
};